package com.hospital.config;

import com.hospital.yang.login.entity.DoctorLogin;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @description：登录拦截
 * @author：zou
 * @date：2021/7/26 0:22
 * @versions：v1.0
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.err.println("开始拦截。。。。。。");
        HttpSession session = request.getSession();
        DoctorLogin doctor = (DoctorLogin) session.getAttribute("doctor");
        System.err.println(doctor);
        if (doctor != null) {
            return true;
        } else {
            response.sendRedirect(request.getContextPath() + "/doctorLogin/toLogin");
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

}
